package android.slc.appbase.api.slc.config;

import android.slc.appbase.repository.local.DbSp;

import com.blankj.utilcode.util.StringUtils;

/**
 * Created by on the way on 2018/9/18.
 */
public class ApiSp {
    //手动
    public final static int CONFIG_MODE_HM = 0;
    //自动
    public final static int CONFIG_MODE_AUTO_CURRENT = 1;
    public final static int CONFIG_MODE_AUTO_LOCAL = 2;

    private final static String SP_NAME = "api_config";
    private final static String KEY_APPLY_CACHE_API = "applyCacheApi";
    private final static String KEY_SERVICE_MODE = "serviceMode";
    private final static String KEY_SERVICE_API_ADDRESS = "serviceApiAddress";

    private final static DbSp dbSp = new DbSp(SP_NAME);

    //apply
    public static boolean isApplyCacheApi() {
        return dbSp.getBoolean(KEY_APPLY_CACHE_API);
    }

    public static void setApplyCacheApi(boolean applyCacheApi) {
        dbSp.putValue(KEY_APPLY_CACHE_API, applyCacheApi);
    }

    //service
    public static int getServiceMode() {
        return getServiceMode(CONFIG_MODE_HM);
    }

    public static int getServiceMode(int defValue) {
        return dbSp.getInt(KEY_SERVICE_MODE, defValue);
    }

    public static void setServiceMode(int value) {
        dbSp.putValue(KEY_SERVICE_MODE, value);
    }

    public static String getServiceApiAddress() {
        return getServiceApiAddress(null);
    }

    public static String getServiceApiAddress(String defValue) {
        return dbSp.getString(KEY_SERVICE_API_ADDRESS, defValue);
    }

    public static void setServiceApiAddress(String value) {
        setServiceApiAddress(value, true);
    }

    public static void setServiceApiAddress(String value, boolean forceSave) {
        if (forceSave || StringUtils.isTrimEmpty(getServiceApiAddress())) {
            dbSp.putValue(KEY_SERVICE_API_ADDRESS, value);
        }
    }

}